package com.comleader.operator.service;


import static com.comleader.operator.pojo.entity.table.UserTableDef.USER;

import org.springframework.stereotype.Service;

import com.comleader.operator.common.response.ResultEnum;
import com.comleader.operator.core.exception.BusinessException;
import com.comleader.operator.mapper.UserMapper;
import com.comleader.operator.pojo.entity.UserEntity;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.crypto.digest.DigestUtil;

/**
 * 用户表 服务层实现。
 *
 * @author 连新景
 * @since 1.0
 */
@Service
public class UserService extends ServiceImpl<UserMapper, UserEntity> {

    public String login(UserEntity userEntity) {
        QueryWrapper queryWrapper = QueryWrapper.create().select().where(USER.USERNAME.eq(userEntity.getUsername()));
        UserEntity user = getOne(queryWrapper);
        // 账号不存在、密码错误
        if (user == null || !user.getPassword().equals(DigestUtil.md5Hex(userEntity.getPassword() + user.getSalt()))) {
            throw new BusinessException(ResultEnum.PASSWORD_NOT_TRUE);
        }
        StpUtil.login(user.getId());
        return user.getName();
    }

}